<template>
  <div class="content">
    <div class="header">
      <h3 class="title">用户列表</h3>
      <el-button type="primary" @click="handleNewClick">新建用户</el-button>
    </div>
    <div class="table">
      <el-table :data="userList" border style="width: 100%">
        <el-table-column align="center" type="selection" width="60px" />
        <el-table-column
          align="center"
          type="index"
          label="序号"
          width="60px"
        />

        <el-table-column
          align="center"
          prop="name"
          label="用户名"
          width="150px"
        />
        <el-table-column
          align="center"
          prop="realname"
          label="真实姓名"
          width="150px"
        />
        <el-table-column
          align="center"
          prop="cellphone"
          label="手机号码"
          width="150px"
        />
        <el-table-column
          align="center"
          prop="enable"
          label="状态"
          width="100px"
        >
          <template #default="scope">
            <el-button
              size="small"
              :type="scope.row.enable ? 'primary' : 'danger'"
              plain
            >
              {{ scope.row.enable ? '启用' : '禁用' }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="createAt" label="创建时间">
          <template #default="scope">
            {{ formatDate(scope.row.createAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="updateAt" label="更新时间">
          <template #default="scope">
            {{ formatDate(scope.row.updateAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="220px">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              icon="Edit"
              text
              @click="handleEditClick(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="small"
              icon="Delete"
              text
              @click="handleDeteleClick(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="usersTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { useSystemStore } from '@/stores/main/system/stytem'
import { storeToRefs } from 'pinia'
import { formatDate } from '@/utils/format'
import { ref } from 'vue'

const emit = defineEmits(['newClick', 'editClick'])

// 发送action请求
const systemStore = useSystemStore()
const currentPage = ref(1)
const pageSize = ref(10)
fetchUserPageData()

// 获取数据
const { userList, usersTotalCount } = storeToRefs(systemStore)
// 分页参数

function handleCurrentChange() {
  fetchUserPageData()
}
function handleSizeChange() {
  fetchUserPageData()
}

// 分页数据请求方法
function fetchUserPageData(formData?: any) {
  // 获取offset、size
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const pageInfo = { size, offset }
  const info = { ...pageInfo, ...formData }
  // 发起网络请求
  systemStore.postUserListAction(info)
}

// 删除
function handleDeteleClick(id: number) {
  systemStore.deleteUserById(id)
}
// 编辑
function handleEditClick(data: any) {
  emit('editClick', data)
}
// 新建
function handleNewClick() {
  emit('newClick')
}

// 暴露方法让父组件调用
defineExpose({ fetchUserPageData })
</script>

<style lang="scss" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;
  .title {
    font-size: 22px;
  }
}
.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }
  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
</style>
